package com.jn.picasso.views;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jn.picasso.R;
import com.jn.picasso.base.BaseActivity;
import com.jn.picasso.contract.MainContract;
import com.jn.picasso.entity.MovieEntity;
import com.jn.picasso.presenter.MainPresenter;
import com.squareup.picasso.Transformation;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements MainContract.IMainView, View.OnClickListener {

    private Button mButton;
    private TextView mText;
    private ImageView mImage;
    private ImageView mImage2;
    private EditText mInputUrl;
    private Button mGenerateButton;
    private MainContract.IMainPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        mPresenter = new MainPresenter(this, this);
        mButton = findViewById(R.id.button);
        mText = findViewById(R.id.text);
        mImage = findViewById(R.id.image);
        mImage2 = findViewById(R.id.image2);
        mInputUrl = findViewById(R.id.inputUrl);
        mGenerateButton = findViewById(R.id.generateButton);

        mButton.setOnClickListener(this);
        mGenerateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = mInputUrl.getText().toString();
                if (!TextUtils.isEmpty(url)) {
                    Intent intent = new Intent(MainActivity.this, QRCodeActivity.class);
                    intent.putExtra("url", url);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            mPresenter.getData();
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {}

    @Override
    public void onHttpFail(String... msg) {}

    @Override
    public void onHttpTimeout() {}

    @Override
    public void onHttpTokenExpiration() {}

    @Override
    public void showData(MovieEntity movie) {
        StringBuffer sb = new StringBuffer();
        sb.append("名称:").append(movie.getResult().getMov_title()).append("\n");
        sb.append("导演:").append(movie.getResult().getMov_director()).append("\n");
        sb.append("年份:").append(movie.getResult().getMov_year()).append("\n");
        sb.append("类型:").append(movie.getResult().getMov_type()).append("\n");
        sb.append("评分:").append(movie.getResult().getMov_rating()).append("\n");
        sb.append("地区:").append(movie.getResult().getMov_area()).append("\n");
        mText.setText(sb.toString());

        Picasso.get()
                .load(movie.getResult().getPoster_url())
                .into(mImage);
        Picasso.get()
                .load(movie.getResult().getMov_pic())
                .into(mImage2);
    }

    public static class BlurTransformation implements Transformation {
        RenderScript rs;

        public BlurTransformation(Context context) {
            rs = RenderScript.create(context);
        }

        @Override
        public Bitmap transform(Bitmap bitmap) {
            Bitmap outputBitmap = Bitmap.createBitmap(bitmap);
            ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            Allocation input = Allocation.createFromBitmap(rs, bitmap);
            Allocation output = Allocation.createFromBitmap(rs, outputBitmap);
            blurScript.setRadius(10.0f);
            blurScript.setInput(input);
            blurScript.forEach(output);
            output.copyTo(outputBitmap);
            rs.destroy();
            input.destroy();
            output.destroy();
            return outputBitmap;
        }

        @Override
        public String key() {
            return "blur";
        }
    }

    public static class GrayTransformation implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            int width = source.getWidth();
            int height = source.getHeight();
            Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
            Canvas c = new Canvas(bmpGrayscale);
            Paint paint = new Paint();
            ColorMatrix cm = new ColorMatrix();
            cm.setSaturation(0);
            ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
            paint.setColorFilter(f);
            c.drawBitmap(source, 0, 0, paint);
            if(source != null && source != bmpGrayscale) {
                source.recycle();
            }
            return bmpGrayscale;
        }

        @Override
        public String key() {
            return "gray";
        }
    }
}
